/*
 * Created on 2005-9-30
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package atom.core2.j2ee.util;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.tagext.BodyTagSupport;

/**
 * @author aps-tlm
 * 
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
//使用方法 Pager.savePagerInfo(request, offset, length, size, url);
//会生成分页信息字符串 并保存到request里面
public class PagerHtmlGen extends BodyTagSupport
{
	private static int MAX_PAGE_INDEX = 10; // 页脚显示多少页

	private static String HEADER = "";

	private int offset;

	private int length;

	private int size;

	private String url;

	public PagerHtmlGen()
	{
		super();
	}

	public static String generate(int offset, int length, int size, String url)
	{
		int pages = 0;
		if (size % length > 0)
		{
			pages = size / length + 1;
		}
		else
		{
			pages = size / length;
		}
		HEADER = "<hr style='color: #FFD39C;'>&nbsp;&nbsp;每页显示&nbsp;<span style='color: #FF4500;font-weight:bold;'>"
				+ length
				+ "</span>&nbsp;条&nbsp;页数&nbsp;<span style='color: #FF4500;font-weight:bold;'>"
				+ pages
				+ "</span>&nbsp;共&nbsp;<span style='color: #FF4500;font-weight:bold;'>"
				+ size + "</span>&nbsp;条&nbsp;";
		if (length < size)
		{
			String pref;
			if (url.indexOf("?") > -1)
			{
				pref = "&";
			}
			else
			{
				pref = "?";
			}
			String header = HEADER;
			if (offset > 0)
			{

				header += "<a href=\""
					+ url
					+ pref
					+ "pager.offset=0"
					+ "&size=" + size  //总计录数，避免二次查询
					+ "\" style='text-decoration:none;color: #FF9900;'><<<首页</a>\n";
				
				
				header += "<a href=\""
						+ url
						+ pref
						+ "pager.offset="
						+ (offset - length)
						+ "&size=" + size
						+ "\" style='text-decoration:none;color: #FF9900;'><<上一页</a>\n";
			}
			int start = 0;
			int radius = MAX_PAGE_INDEX * length;
			if (offset < radius)
			{
				start = 0;
				// }else if(offset > size-radius){
				// start = offset - radius;
			}
			else if (offset / radius > 0)
			{
				start = offset / radius * radius;
				// start = (size/length-MAX_PAGE_INDEX)*length;
			}
			for (int i = start; i < size && i < start + MAX_PAGE_INDEX * length; i += length)
			{
				if (i == offset)
				{
					header += "&nbsp;<font color=red>" + (i / length + 1)
							+ "</font>\n";
				}
				else
				{
					header += "&nbsp;<a href=\"" + url + pref + "pager.offset="
							+ i
							+ "&size=" + size
							+ "\" style='text-decoration:none;color: #FF9900;'>"
							+ (i / length + 1) + "</a>\n";
				}
			}
			if (offset < size - length)
			{
				header += "&nbsp;<a href=\""
						+ url
						+ pref
						+ "pager.offset="
						+ ((int) offset + (int) length)
						+ "&size=" + size
						+ "\" style='text-decoration:none;color: #FF9900;'>下一页>></a>\n";

				//尾页记录数
				int endCount = size%length;
				if (endCount == 0)
				{
					endCount = length;
				}
				
				header += "&nbsp;<a href=\""
					+ url
					+ pref
					+ "pager.offset="
					+ (size - endCount)
					+ "&size=" + size
					+ "\" style='text-decoration:none;color: #FF9900;'>尾页>>></a>\n";
			}
			header += "";
			return header;
		}
		else
		{
			return "";
		}
	}

	/**
	 * @param request
	 * @return
	 */
	public static int getPageOffset(HttpServletRequest request)
	{
		int offset;
		String pageOffset = request.getParameter("pager.offset");
		if (pageOffset == null || pageOffset.equals(""))
		{
			offset = 0;
		}
		else
		{
			offset = Integer.parseInt(pageOffset);
		}
		return offset;
	}

	/**
	 * @param request
	 * @param offset
	 * @param length
	 * @param size
	 * @param url
	 */
	public static void savePagerInfo(HttpServletRequest request, int offset,
			int length, int size, String url)
	{
		String pagerHeader = PagerHtmlGen.generate(offset, length, size, url);
		request.setAttribute("pager", pagerHeader);
	}

}
